<?php
namespace app\admin\controller;

use app\BaseController;

use app\common\model\AtlasModel;
use app\common\model\AtlasCategoryModel;
class Atlas extends BaseController{
    
    public function list(){
        $keyword = $this->request->param('keyword/s','');
        $category_id = $this->request->param('category_id/d',0);
        $where=[
            ['firm_id','=',$this->request->firm_id],
            ['is_delete','=',0]
        ];
        
        if(!empty($keyword)){
            $where[] = ['title','LIKE','%'.$keyword.'%'];
        }
        if(!empty($category_id)){
            $where[] = ['category_id','=',$category_id];
        }

        $list = AtlasModel::where($where)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order(['sort' => 'desc', 'atlas_id' => 'desc'])
            ->select()
            ->append(['category']);
        $total = AtlasModel::where($where)->count('atlas_id');
        return success([
            'list' => $list,
            'total' => $total
        ]);
    }
    
    public  function detail(){
        $atlas_id = $this->request->param('atlas_id/d',0);
        if(empty($atlas_id)){
            return error('错误操作');
        }
        if(null === $atlasModel = AtlasModel::find($atlas_id)){
            return error('内容不存在');
        }
        if($atlasModel->firm_id != $this->request->firm_id){
             return error('没有权限');
        }
        return success($atlasModel);
    }
    
    public function save(){

        $atlas_id = $this->request->param('atlas_id/d',0);

        $title = $this->request->param('title/s','');

        $category_id  = $this->request->param('category_id/d',0);

        $thumbnail  = $this->request->param('thumbnail/s','');

        $desc  = $this->request->param('desc/s','');

        $atlas = $this->request->param('atlas/a','',);
        
        $sort  = $this->request->param('sort/d',0);

        if(empty($title)){
            return error('请填写标题');
        }
        if(empty($category_id)){
            return error('请选择文章分类');
        }
        if(empty($thumbnail)){
            $thumbnail = '11111';
            // return error('请上传缩略图片');
        }
        if(empty($desc)){
            return error('请输入简介');
        }
        if(empty($atlas)){
            $atlas = [
                'aaa'=>'cccc'
            ];
            // return error('请编辑内容');
        }
        if(empty($atlas_id)){
            $atlasModel = new AtlasModel();
            $atlasModel->firm_id = $this->request->firm_id;
        }else{
            if(null === $atlasModel = AtlasModel::find($atlas_id)){
                return error('错误操作');
            }
            if($atlasModel->firm_id != $this->request->firm_id){
                 return error('请选择要编辑的内容');
            }
        }

        $atlasModel->title = $title;
        $atlasModel->category_id  = $category_id;
        $atlasModel->desc = $desc;
        $atlasModel->thumbnail = $thumbnail;
        $atlasModel->atlas = $atlas;
        $atlasModel->sort = $sort;
        $atlasModel->save();

        return success('保存成功');
    }
    
    public function delete(){
        $atlas_id = $this->request->param('atlas_id/d',0);
        if(empty($atlas_id)){
            return error('错误操作');
        }
        if(null === $atlasModel = AtlasModel::find($atlas_id)){
            return error('文章不存在');
        }
        if($atlasModel->firm_id != $this->request->firm_id){
            return error('没有权限');
        }
        $atlasModel->is_delete = 1;
        $atlasModel->save();
        return success('删除成功');
    }


    //文章分类
    public function categoryList()
    {
        $keyword = $this->request->param('keyword/s','');
        $where=[
            ['firm_id','=',$this->request->firm_id]
        ];
        if(!empty($keyword)){
            $where[] = ['category_name','LIKE','%'.$keyword.'%'];
        }
        $list = AtlasCategoryModel::where($where)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('sort','desc')
            ->select();
        $total = AtlasModel::where($where)->count('category_id');
        return success([
            'list' => $list,
            'total' => $total
        ]);
    }

    //文章全部分分类
    public function categoryAllList()
    {
        $keyword = $this->request->param('keyword/s','');
        $where = [
            ['firm_id','=',$this->request->firm_id]
        ];
        if(!empty($keyword)){
            $where[] = ['category_name','LIKE','%'.$keyword.'%'];
        }
        $list = AtlasCategoryModel::field('category_name,category_id')->where($where)
            ->order('sort','desc')
            ->select();
        return success($list);
    }
   
    public function categorySave()
    {
        $category_id = $this->request->param('category_id/d',0);
        $category_name = $this->request->param('category_name/s','');
        $sort = $this->request->param('sort/d',0);

        if(empty($category_name)){
            return error('请填写分类名称');
        }

        if(empty($category_id)) {
            $category = new AtlasCategoryModel();
            $category->firm_id = $this->request->firm_id;
        }else{
            if(null === $category = AtlasCategoryModel::find($category_id))  return error('不存在');
            if($category->firm_id != $this->request->firm_id)  return error('不存在');
        }

        $category->category_name = $category_name;
        $category->sort = $sort;

        $category->save();

        return success('保存成功');
    }

    public function categoryDelete()
    {
        $category_id = $this->request->param('category_id/d',0);

        if(null === $category = AtlasCategoryModel::find($category_id))  return error('不存在');

        if($category->firm_id != $this->request->firm_id){
            return error('没有权限');
        }
        $category->is_delete = 1;
        $category->save();
        return $this->success('删除成功');
    }
    
}